<template>
  <view class="work-page">
    <view class="signature">
      <view class="title">{{
        workMainData?.permit_user_ids?.includes(currentUser[0]) ? '签名' : '没有权限签名'
      }}</view>
      <wd-signature
        @confirm="confirmSig"
        @clear="clear"
        :export-scale="2"
        v-if="workMainData?.permit_user_ids?.includes(currentUser[0])"
      />
      <!-- <wd-img v-if="img.tempFilePath" mode="widthFix" width="100%" :src="img.tempFilePath" /> -->
      <wd-img
        mode="widthFix"
        width="100%"
        :src="workMainData?.signature"
        v-if="workMainData?.signature"
      />
    </view>
  </view>
</template>

<script setup>
// const props = defineProps({
//   currentUser: Array,
// })
import { pathToBase64 } from 'image-tools'
import { dateShow } from '@/utils/utils.js'
import { ref, onMounted } from 'vue'
import { onShow } from '@dcloudio/uni-app'
//下面与template没有关系的脚本部分
import { useWorkmainStore, useWorksubStore, useUserStore, useWorktoolStore } from '@/stores'
const currentUser = ref([])
const workMain = useWorkmainStore()
const worksubStore = useWorksubStore()
const userStore = useUserStore()
// 当前work-main数据
const workMainData = ref({})
const workMainId = ref()
// 签名相关
const img = ref({})
async function confirmSig(result) {
  //获取android图片存放的绝对路径
  //plus.io.getImageInfo({
  //  src: result.tempFilePath,
  //  success: (res) => {
  //    const tempFilePath = res.path
  //    console.log(tempFilePath)
  //    // image-tools插件实现 可用于uni-app、微信小程序、5+APP、浏览器（需允许跨域）
  //    pathToBase64(tempFilePath).then((base64) => {
  //      console.log(base64)
  //      workMainData.value.signature = base64
  //      workMainData.value.write_date = dateShow()[1]
  //    })
  //  },
  //})

  // console.log(result.tempFilePath) //android时： _doc/uniapp_temp_1742812818981/canvas/17428128264950.png
  //h5时，result.tempFilePath已经时base64,pathToBase64转换还是Base64.但是在APP,则会将路径转换成base64
  pathToBase64(result.tempFilePath).then((base64) => {
    // console.log(base64)
    workMainData.value.signature = base64
    workMainData.value.write_date = dateShow()[1]
  })
  ////下面是h5原来的做法：
  // const src64 = result.tempFilePath.replace(/\s/g, '')
  // workMainData.value.signature = src64
  // workMainData.value.write_date = dateShow()[1]
}

function clear() {
  // workMainData.value.signature = null
  img.value = {}
}
// onShow(async () => {
//   currentUser.value = [userStore.userInfo.uid, userStore.userInfo.name]
//   //获取worksubData
//   // workMainId.value = worksubStore.currentWorkMainId
//   // worksubData.value = worksubStore.records[workMainId.value]
//   //获取workMainData
//   const workMainIndex = workMain.records.findIndex((item) => item.id == workMainId.value)
//   workMainData.value = workMain.records[workMainIndex]
// })
onMounted(() => {
  currentUser.value = [userStore.userInfo.uid, userStore.userInfo.name]
  //获取worksubData
  workMainId.value = worksubStore.currentWorkMainId
  // worksubData.value = worksubStore.records[workMainId.value]
  //获取workMainData
  const workMainIndex = workMain.records.findIndex((item) => item.id == workMainId.value)
  workMainData.value = workMain.records[workMainIndex]
})
</script>

<style lang="scss" scoped>
.work-page {
  .margin-top-bottom {
    margin: 10px 0;
  }
  .signature {
    margin: 10px;
  }
}
</style>
